(function() {

z._keyword_index = 0;
z._nm_index = 0;

z.knm = function (wrap, node_form, type) {
    this.wrap = wrap;
    this.node_form = node_form;
    type = type || 'prev';
    this.type = type;
    this.requires = [];
    this.kns = {};
    
    //this.index = 0;
    var othis = this;
    if (node_form) {
        z.addEvent(node_form, 'submit', function (e) {
            var requires = othis.requires;
            var length = requires.length;
            for (var i=0; i < length; i++) {
                var key = requires[i];
                if ( ! this[key] || z.trim(this[key].value) == '') {
                    alert(key + ' cannot be empty');
                    if(e&&e.preventDefault){
                        e.preventDefault();
                        e.stopPropagation();
                    }else{
                        event.cancelBubble=true;
                        event.returnValue=false;
                    }
                   
                    return false;
                }
            }
            
            for (var index in othis.kns) {
                var kn = othis.kns[index];
                var keyword = z.trim(kn.keyword.value);
                if (kn.keyword.changed && keyword != '') {
                    othis.set('keyword[' + index + ']', keyword);
                }
                var ns = kn.n_wrap.childNodes;
                var type = othis.type;
                for (var i = 0; i < ns.length; i++) {
                    othis.set(type + '[' + index + '][]', ns[i].node_id);
                }
            }
        });
    }
    /*node_form.onsubmit = function () {
        
    };*/
};

z.knm.prototype = {
    add_kn: function(keyword, node) {
        var kn;
        keyword = keyword || '';
        if (keyword != '') {
            for (var index in this.kns) {
                var _kn = this.kns[index];
                if (keyword == _kn.keyword.value) {
                    kn = _kn;
                    break;
                }
            }
        }
        
        if ( ! kn) {
            var index = z._keyword_index + 1;
            z._keyword_index = index;
            var othis = this;
            kn = this._create_kn(index, keyword);
            this.kns[index] = kn;
        }
        
        if (node) {
            this._add_node(kn, node);
        }
    },
    
    
    set: function(key, value) {
        var form = this.node_form;
        if ( ! form[key]) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = key;
            form.appendChild(input);
        }
        form[key].value = value;
    },
    
    
    add_require: function(key) {
        this.requires.push(key);
    },
    
    
    _add_node: function (kn, node) {
        if ( ! node.id) return;
        if ( ! node.caption) return;
        var item = document.createElement('div');
        item.className = 'item';
        item.node_id = node.id;
        
        var caption = document.createElement('h3');
        caption.innerHTML = node.caption;
        item.appendChild(caption);
        
        if (node.keywords) {
            var keywords = document.createElement('p');
            keywords.innerHTML = node.keywords;
            item.appendChild(keywords);
        }
        if (node.description) {
            var description = document.createElement('p');
            var str = node.description;
            if (str.length > 100) {
                str = str.substring(0, 100) + '...';
            }
            description.innerHTML = str;
            item.appendChild(description);
        }
        kn.n_wrap.appendChild(item);
        //this.set(this.type + '_nm[]' 
    },
    
    
    _create_kn: function (index, keyword) {
        var othis = this;
        var kn =document.createElement('div');
        var k = document.createElement('div');
        var n = document.createElement('div');
        
        kn.appendChild(k);
        kn.appendChild(n);
        
        kn.n_wrap = n;
        
        this.wrap.appendChild(kn);
        kn.className = 'kn';
        k.className = 'k';
        n.className = 'n';
            
       if (this.node_form) {
            var bt = document.createElement('a');
            var img = document.createElement('img');
            var text = document.createElement('input');
            k.appendChild(bt);
            k.appendChild(text);
    
            bt.className = 'bt';
            bt.href = 'javascript:;';
            img.src = z.cdn_url('img/icon/arrow3_e.png');
            bt.appendChild(img);
            
            text.className = 'text';
            text.name = 'keyword';
            text.type = 'text';
            text.index = index;
            text.value = keyword;
            text.onchange = function () {
                text.changed = true;
                if (this.index == z._keyword_index) {
                    othis.add_kn();
                }
            };
            
            kn.keyword = text;
        } else {
            k.innerHTML = keyword;
            k.value = keyword;
            kn.keyword = k;
        }
        //text.focus();
        
        return kn;
    }
};

}) ();

/*
 <div class="kn">
    <div class="k">
        <a class="bt" href="javascript:;" onclick="add_keyword();"><img src="<?php echo tos_cdn_url('img/icon/arrow3_e.png');?>" /></a>
        <input id="keyword_input" type="text" class="text" value="" />
    </div>
    <div class="n">
    
    </div>
    </div>
 
 */